package com.asj.house.service.impl;

import com.asj.house.entity.*;
import com.asj.house.mapper.HouseRoomMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.returns.RespTable;
import com.asj.house.service.*;
import com.asj.house.vo.param.RoomParam;
import com.asj.house.vo.param.UserParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 房屋信息表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-16
 */
@Service
public class HouseRoomServiceImpl extends ServiceImpl<HouseRoomMapper, HouseRoom> implements HouseRoomService {

    @Autowired
    private HouseRoomDetailService houseRoomDetailService;

    @Autowired
    private HouseRoomResourceService houseRoomResourceService;

    @Autowired
    private HouseRoomMapService houseRoomMapService;

    @Autowired
    private HouseHitsService houseHitsService;

    @Override
    public RespTable<List<HouseRoom>> listHouseRoom(RoomParam roomParam) {
        QueryWrapper<HouseRoom> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(roomParam.getName())) {
            queryWrapper.lambda().like(HouseRoom::getName, roomParam.getName());
        }
        queryWrapper.lambda().eq(HouseRoom::getDeleted, 0)
                .orderByDesc(HouseRoom::getId);

        IPage<HouseRoom> iPage = new Page<>(roomParam.getPage(), roomParam.getLimit());
        IPage<HouseRoom> page = super.page(iPage, queryWrapper);

        return new RespTable<>(RespCode.OK, page.getRecords(), page.getTotal());
    }

    @Override
    public RespEntity insertHouseRoom(HouseRoom houseRoom) {
        //数据处理
        if (StringUtils.isNotBlank(houseRoom.getOnTime())) {
            String[] split = houseRoom.getOnTime().split(" - ");
            houseRoom.setOnTime(split[0]);
            houseRoom.setOffTime(split[1]);
        }

        //新增房源主表
        baseMapper.insert(houseRoom);
        //生成房源主键id
        Integer roomId = houseRoom.getId();

        //新增房源详细信息表
        HouseRoomDetail houseRoomDetail = new HouseRoomDetail(roomId);
        houseRoomDetailService.save(houseRoomDetail);

        //新增房源信息表
        HouseRoomResource houseRoomResource = new HouseRoomResource(roomId);
        houseRoomResourceService.save(houseRoomResource);

        //新增房源地图表
        HouseRoomMap houseRoomMap = new HouseRoomMap(roomId);
        houseRoomMapService.save(houseRoomMap);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity updateHouseRoom(HouseRoom houseRoom) {
        //数据处理
        if (StringUtils.isNotBlank(houseRoom.getOnTime())) {
            String[] split = houseRoom.getOnTime().split(" - ");
            houseRoom.setOnTime(split[0]);
            houseRoom.setOffTime(split[1]);
        }
        baseMapper.updateById(houseRoom);
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity deleteHouseRoom(Integer roomId) {

        //删除房源主表数据
        baseMapper.deleteById(roomId);

        //删除房源详细信息表数据
        QueryWrapper<HouseRoomDetail> detailQueryWrapper = new QueryWrapper<>();
        detailQueryWrapper.lambda().eq(HouseRoomDetail::getRoomId, roomId);
        houseRoomDetailService.remove(detailQueryWrapper);

        //删除房源信息表数据
        QueryWrapper<HouseRoomResource> resourceQueryWrapper = new QueryWrapper<>();
        resourceQueryWrapper.lambda().eq(HouseRoomResource::getRoomId, roomId);
        houseRoomResourceService.remove(resourceQueryWrapper);

        //删除房源地图信息表数据
        QueryWrapper<HouseRoomMap> mapQueryWrapper = new QueryWrapper<>();
        mapQueryWrapper.lambda().eq(HouseRoomMap::getRoomId, roomId);
        houseRoomMapService.remove(mapQueryWrapper);

        //删除房源点击量表数据
        QueryWrapper<HouseHits> hitsQueryWrapper = new QueryWrapper<>();
        hitsQueryWrapper.lambda().eq(HouseHits::getRoomId, roomId);
        houseHitsService.remove(hitsQueryWrapper);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity deleteBatchHouseRoom(Integer[] roomIds) {

        //删除房源主表数据
        baseMapper.deleteBatchIds(Arrays.asList(roomIds));

        for (Integer roomId : roomIds) {
            //删除房源详细信息表数据
            QueryWrapper<HouseRoomDetail> detailQueryWrapper = new QueryWrapper<>();
            detailQueryWrapper.lambda().eq(HouseRoomDetail::getRoomId, roomId);
            houseRoomDetailService.remove(detailQueryWrapper);

            //删除房源信息表数据
            QueryWrapper<HouseRoomResource> resourceQueryWrapper = new QueryWrapper<>();
            resourceQueryWrapper.lambda().eq(HouseRoomResource::getRoomId, roomId);
            houseRoomResourceService.remove(resourceQueryWrapper);

            //删除房源地图表数据
            QueryWrapper<HouseRoomMap> mapQueryWrapper = new QueryWrapper<>();
            mapQueryWrapper.lambda().eq(HouseRoomMap::getRoomId, roomId);
            houseRoomMapService.remove(mapQueryWrapper);
        }

        return new RespEntity(RespCode.SUCCESS);
    }
}
